layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 打包复核管理
     */
    var PackReview = {
        tableId: "packReviewTable"
    };

    /**
     * 初始化表格的列
     */
    PackReview.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'trackNumber', sort: true, title: '运单号'},
            {field: 'orderType', sort: true, title: '订单类型'},
            {field: 'sendOutTime', sort: true, title: '发货时间'},
            {field: 'reviewTime', sort: true, title: '复核时间'},
            {field: 'reviewStatus', sort: true, title: '复核状态'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createBy', sort: true, title: '创建人'},
            {field: 'updateTime', hide: true, sort: true, title: '修改时间'},
            {field: 'updateBy', hide: true, sort: true, title: '修改人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    PackReview.search = function () {
        var queryData = {};
        queryData['orderType'] = $("#orderType").val();
        queryData['trackNumber'] = $("#trackNumber").val();
        table.reload(PackReview.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 $('#btnSearch').click(function () {
                     PackReview.search();
                 });
                 // 添加按钮点击事件
                 $('#btnAdd').click(function () {
                     PackReview.openAddDlg();
                 });
                 // 导出excel
                 $('#btnExp').click(function () {
                     PackReview.exportExcel();
                 });
                 // $('#btnExpimport').click(function () {
                 //     PackReview.importExcel();
                 // });
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    PackReview.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/packReview/add';
    };

    /**
     * 导出excel按钮
     */
    PackReview.exportExcel = function () {
        var checkRows = table.checkStatus(PackReview.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };
    PackReview.importExcel = function () {
        layer.open({
            type: 2,
            title: '运单导入',
            area: ['1000px', '600px'],
            anim: 5,
            content: Feng.ctxPath + '/packReview/toimportExcel',
            // end: function () {
            //     table.reload(PackReview.tableId);
            // }
        });
    };


    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    PackReview.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/packReview/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    PackReview.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/packReview/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(PackReview.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + PackReview.tableId,
        url: Feng.ctxPath + '/packReview/list',
        page: true,
        limits: [10,50,100,500,1000,5000,10000],
        height: 500,
        toolbar: '#toolbarfrom',
        cellMinWidth: 200,
        cols: PackReview.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        PackReview.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        PackReview.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        PackReview.exportExcel();
    });
    $('#btnExpimport').click(function () {
        PackReview.importExcel();
    });
    // 工具条点击事件
    table.on('tool(' + PackReview.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            PackReview.openEditDlg(data);
        } else if (layEvent === 'delete') {
            PackReview.onDeleteItem(data);
        }
    });

    // 单击行事件
    table.on('row(' + PackReview.tableId + ')', function (obj) {
        var data = obj.data;
        $('div[lay-id="' + PackReview.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
        $('div[lay-id="' + PackReview.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
        packReview(data);
    });



    /**
     * 打包复核详情管理
     */
    var PackReviewDetails = {
        tableId: "packReviewDetailsTable"
    };

    /**
     * 初始化表格的列
     */
    PackReviewDetails.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'packReviewId', sort: true, title: '主表id'},
            {field: 'trackNumber', sort: true, title: '运单号'},
            {field: 'goodsName', sort: true, title: '商品名称'},
            {field: 'goodsCode', sort: true, title: '商品编码'},
            {field: 'goodsNum', sort: true, title: '商品69码'},
            {field: 'reviewTime', sort: true, title: '复核时间'},
            {field: 'reviewStatus', sort: true, title: '复核状态'},
            {field: 'reviewer', sort: true, title: '复核人'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createBy', sort: true, title: '创建人'},
            {field: 'updateTime', sort: true, title: '修改时间'},
            {field: 'updateBy', sort: true, title: '修改人'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };



    function packReview(data){
        var trackNumber;
        if (data!=null) {
            $("#trackNumber").val(data.trackNumber);
            trackNumber = data.trackNumber;
        } else {
            trackNumber = 'nodata';
        }
        // 渲染商品明细表格

        var tableResult = table.render({
            elem: '#' + PackReviewDetails.tableId,
            url: Feng.ctxPath + '/packReviewDetails/list?trackNumber=' + trackNumber,
            page: true,
            limit: 20,
            height: 400,
            toolbar: '#toolbarfromDetailed',
            cellMinWidth: 100,
            cols: PackReviewDetails.initColumn()
        });
        /**
         * 点击查询按钮
         */
        PackReviewDetails.search = function () {
            var queryData = {};
            queryData['condition'] = $("#condition2").val();
            table.reload(PackReviewDetails.tableId, {
                where: queryData, page: {curr: 1},
                done:function () {
                    // 搜索按钮点击事件
                    $('#btnSearchDetailed').click(function () {
                        PackReviewDetails.search();
                    });
                    // 添加按钮点击事件
                    $('#btnAddDetailed').click(function () {
                        PackReviewDetails.addDetailed();
                    });
                }
            });
        };

        PackReviewDetails.addDetailed = function () {
            layer.open({
                type: 2,
                title: '明细录入',
                area: ['1000px', '750px'],
                anim: 5,

                content: Feng.ctxPath + '/packReviewDetails/add?trackNumber=' + data.trackNumber+"&packReviewId="+data.id,
                end: function () {
                    packReview(data);
                }
            });
        };

        // 工具条点击事件
        table.on('tool(' + PackReviewDetails.tableId + ')', function (obj) {
            var data2 = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'editDetailed') {
                PackReviewDetails.openEditDlg(data2);
            } else if (layEvent === 'deleteDetailed') {
                PackReviewDetails.onDeleteItem(data2);
            } else if (layEvent === 'moveDetailed') {
                PackReviewDetails.openMoveDlg(data2);
            }
        });

        // 修改按钮点击事件
        PackReviewDetails.openEditDlg = function (data2) {
            layer.open({
                type: 2,
                title: '明细修改',
                area: ['1000px', '750px'],
                anim: 5,
                content: Feng.ctxPath + '/outOrderDetailed/edit?id='+data2.id+"&detailedOutNumber="+data.outOrderNumber+"&outCustomerId="+data.outCustomerId,
                end: function () {
                    packReview(data);
                }
            });
        };

        // 修改按钮点击事件
        PackReviewDetails.openMoveDlg = function (data2) {
            layer.open({
                type: 2,
                title: '移动明细到其他订单',
                area: ['500px', '950px'],
                anim: 5,
                content: Feng.ctxPath + '/outOrderDetailed/moveOpen?id='+data2.id,
                end: function () {
                    PackReviewDetails.search();
                }
            });
        };

        // 删除按钮点击事件
        PackReviewDetails.onDeleteItem = function (data2) {
            var operation = function () {
                var ajax = new $ax(Feng.ctxPath + "/outOrderDetailed/delete", function (data2) {
                    Feng.success("删除成功!");
                    packReview(data);
                }, function (data) {
                    Feng.error("删除失败!" + data2.responseJSON.message + "!");
                });
                ajax.set("id", data2.id);
                ajax.start();
            };
            Feng.confirm("是否删除?", operation);
        };

        // 搜索按钮点击事件
        $('#btnSearchDetailed').click(function () {
            PackReviewDetails.search();
        });

        $('#btnAddDetailed').click(function () {
            PackReviewDetails.addDetailed();
        });

    }

});






